package iha.auh.kds.aba.painratingapp;

import iha.auh.kds.aba.painratingapp.Models.ChildModel;
import android.os.Bundle;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.support.v4.content.LocalBroadcastManager;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class ConfirmScoringActivity extends Activity {
	private String score;
	private ChildModel child;
	private Button confirmBtn;
	private ProgressDialog pd;
	
	private BroadcastReceiver dataReadyFromService =  new BroadcastReceiver() {
		@Override
        public void onReceive(Context context, Intent intent) {
			String action = intent.getAction();
			pd.hide();
			if (action.equalsIgnoreCase(DataService.SAVE_RATING)) {
				String response = intent.getExtras().getString("response");
				if (response.equalsIgnoreCase("success")) {
					Toast.makeText(ConfirmScoringActivity.this, "Scoring blev gemt", Toast.LENGTH_SHORT).show();
					Intent gotoGraduationLineIntent = new Intent(ConfirmScoringActivity.this, GraduationLineActivity.class);
					startActivity(gotoGraduationLineIntent);
				}
				else if (response.equalsIgnoreCase("failure")) {
					Toast.makeText(ConfirmScoringActivity.this, "Der opstod en fejl", Toast.LENGTH_SHORT).show();
					confirmBtn.setText("Proev igen");
				}
			}
        }
	};
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_confirm_scoring);
		
		confirmBtn = (Button) findViewById(R.id.confirm_btn);
		
		TextView scoreTextView = (TextView) findViewById(R.id.score_textview);
		TextView childNameTextView = (TextView) findViewById(R.id.child_name_label);
		TextView childCprTextView = (TextView) findViewById(R.id.child_cpr_label);
		
		pd = new ProgressDialog(this);
		child = ChildSingleton.getChild();
		
		childNameTextView.setText(child.Name);
		childCprTextView.setText(child.CPR);
		
		Intent intent = getIntent();
		if (intent != null) {
			score = intent.getExtras().getString("score");
			scoreTextView.setText(score);
		}
	}

	@Override
	protected void onResume() {
		super.onResume();
		LocalBroadcastManager.getInstance(this).registerReceiver(dataReadyFromService, new IntentFilter(DataService.SAVE_RATING));
	}
	
	@Override
	protected void onPause() {
		super.onPause();
		LocalBroadcastManager.getInstance(this).unregisterReceiver(dataReadyFromService);
	}
	
	public void confirmScore(View view) {
		pd.setMessage("Sender data");
		pd.show();
		
		EditText noteInput = (EditText) findViewById(R.id.note_edittext);
		String note = noteInput.getText().toString();
		
		note = Utilities.encodeString(note);
		
		Intent serviceIntent = new Intent(this, DataService.class);
		//String url = "http://painratingrestful.cloudapp.net/api/painrating/setpainscore?cpr=" + child.CPR  + "&score=" + score + "&note=" + note + "";
		String url = "http://painratingrestful.cloudapp.net/api/painrating/setpainscorepost?cpr=" + child.CPR  + "&score=" + score + "&note=" + note + "";
		serviceIntent.putExtra("save_rating_url", url);
		serviceIntent.putExtra("http_method", "post");
		startService(serviceIntent);
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.confirm_scoring, menu);
		return true;
	}

}
